package com.sayyi.demo.swagger.service.impl;

import com.sayyi.demo.swagger.domain.Employee;
import com.sayyi.demo.swagger.service.EmployeeService;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.atomic.AtomicLong;

/**
 * @author xuchuang
 * date 2021/12/23 4:42 PM
 */
@Service
public class EmployeeServiceImpl implements EmployeeService {

    private final AtomicLong idUtil = new AtomicLong(0);

    private final Map<Long, Employee> employeeCache = new ConcurrentHashMap<>();

    @Override
    public Employee getById(long id) {
        return employeeCache.get(id);
    }

    @Override
    public long save(Employee employee) {
        employee.setId(idUtil.incrementAndGet());
        employeeCache.put(employee.getId(), employee);
        return employee.getId();
    }
}
